package com.employmentapp;

import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;

public class MainActivity extends AppCompatActivity {

    private final String mTag = "MainActivity@" + System.identityHashCode(this);
    private MainAdapter mMainAdapter;
    private ViewPager2 mPager;

    private TabLayout mBottomTab;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mPager = findViewById(R.id.pager);
        mBottomTab = findViewById(R.id.bottom_tab);

        mMainAdapter = new MainAdapter(this);
        mPager.setAdapter(mMainAdapter);

        mBottomTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                // 点击底部 tab 切换 ViewPager
                int position = tab.getPosition();
                Log.i(mTag, "onTabSelected position:" + position + ", tab:" + tab.getText());
                switch (position) {
                    // 首页
                    case 0:
                        if (mPager.getCurrentItem() != 0) {
                            mPager.setCurrentItem(0, false);
                        }
                        break;
                    // 列表
                    case 1:
                        if (mPager.getCurrentItem() != 1) {
                            mPager.setCurrentItem(1, false);
                        }
                        break;
                    // 我的
                    case 2:
                        if (mPager.getCurrentItem() != 2) {
                            mPager.setCurrentItem(2, false);
                        }
                        break;
                    default:
                        throw new IllegalArgumentException("unexpected position:" + position);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });

        mPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                // 滚动或者手动切换 ViewPager 时，切换底部 tab 选中状态
                Log.i(mTag, "onPageSelected position:" + position);
                switch (position) {
                    // 首页
                    case 0:
                        if (mBottomTab.getSelectedTabPosition() != 0) {
                            mBottomTab.selectTab(mBottomTab.getTabAt(0));
                        }
                        break;
                    // 列表
                    case 1:
                        if (mBottomTab.getSelectedTabPosition() != 1) {
                            mBottomTab.selectTab(mBottomTab.getTabAt(1));
                        }
                        break;
                    // 我的
                    case 2:
                        if (mBottomTab.getSelectedTabPosition() != 2) {
                            mBottomTab.selectTab(mBottomTab.getTabAt(2));
                        }
                        break;
                    default:
                        throw new IllegalArgumentException("unexpected position:" + position);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private static class MainAdapter extends FragmentStateAdapter {

        public MainAdapter(@NonNull FragmentActivity fragmentActivity) {
            super(fragmentActivity);
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            switch (position) {
                // 首页
                case 0:
                    return new HomeFragment();
                // 列表
                case 1:
                    return new ListFragment();
                // 我的
                case 2:
                    return new MineFragment();
                default:
                    throw new IllegalArgumentException("unexpected position:" + position);
            }
        }

        @Override
        public int getItemCount() {
            return 3;
        }
    }

}